package com.aiit.service.impl;

import com.aiit.constant.MessageConstant;
import com.aiit.constant.PasswordConstant;
import com.aiit.constant.StatusConstant;
import com.aiit.context.BaseContext;
import com.aiit.dto.EmployeeDTO;
import com.aiit.dto.EmployeeLoginDTO;
import com.aiit.dto.EmployeePageQueryDTO;
import com.aiit.entity.Employee;
import com.aiit.exception.AccountLockedException;
import com.aiit.exception.AccountNotFoundException;
import com.aiit.exception.PasswordErrorException;
import com.aiit.exception.VerificationErrorException;
import com.aiit.mapper.EmployeeMapper;
import com.aiit.result.PageResult;
import com.aiit.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.annotation.WebServlet;
import java.time.LocalDateTime;
import java.util.List;

@Service
@WebServlet("/verifyCode")
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO, String storedVerificationCode) {
        String username = employeeLoginDTO.getUsername();
        String password = employeeLoginDTO.getPassword();
        String verification = employeeLoginDTO.getVerification();

        Employee employee = employeeMapper.getByUsername(username);

        if (employee == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(employee.getPassword())) {
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (employee.getStatus() == StatusConstant.DISABLE) {
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        if (storedVerificationCode == null || !storedVerificationCode.equalsIgnoreCase(verification)) {
            throw new VerificationErrorException(MessageConstant.VERIFICATION_ERROR);
        }
        return employee;
    }

    /**
     * 分页
     *
     * @param employeePageQueryDTO
     * @return
     */
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());

        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);

        long total = page.getTotal();
        List<Employee> records = page.getResult();

        return new PageResult(total, records);

    }


    public void update(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.update(employee);
    }

    public void save(EmployeeDTO employeeDTO) {
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        Employee employee = new Employee();

        //对象属性拷贝
        BeanUtils.copyProperties(employeeDTO, employee);

        //设置账户的状态，默认正常状态1表示正常0表示锁定
        employee.setStatus(StatusConstant.ENABLE);

        //设置密码，默认密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        //设置当前记录的创建时间和修改时间
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //设置当前记录创建人id和修改人的id
        // TODO 后期需要改为当前登录用户的id
        employee.setCreateUser(BaseContext.getCurrentId());
        employee.setUpdateUser(BaseContext.getCurrentId());

        employeeMapper.insert(employee);

    }


    public void deleteEmployee(Long id) {

        employeeMapper.deleteById(id);

    }
}



